package Test;

import Mysinglelist.mysinglelist;

public class test1 {
    //合并两个有序链表
    public static mysinglelist.ListNode merge1(mysinglelist listA, mysinglelist listB) {
        mysinglelist.ListNode newHead = new mysinglelist.ListNode();
        mysinglelist.ListNode currentHead = newHead;
//        if (listA.head.val >= listB.head.val) {
//            newHead.next = listB.head;
//            currentHead.next = listB.head;
//        } else {
//            newHead.next = listA.head;
//            currentHead.next = listA.head;
//        }
        while (listB.head != null && listA.head != null) {
            if(listA.head.val>= listB.head.val){
                currentHead.next=listB.head;
                currentHead=currentHead.next;
                listB.head=listB.head.next;
            }
            else{
                currentHead.next=listA.head;
                currentHead=currentHead.next;
                listA.head=listA.head.next;
            }
        }
        if(listA.head!=null){
            currentHead.next=listA.head;
        }
        else {
            currentHead.next=listB.head;

        }
        return newHead.next;

    }
    public static mysinglelist.ListNode merge2(mysinglelist.ListNode headA, mysinglelist.ListNode headB) {
        mysinglelist.ListNode newHead = new mysinglelist.ListNode(-1);
        mysinglelist.ListNode tmpHead = newHead;
//        if (headA.val >= headB.val) {
//            newHead.next = headB;
//            tmpHead.next = headB;
//        } else {
//            newHead.next = headA;
//            tmpHead.next = headA;
//        }
        while (headA != null &&  headB!= null) {
            if(headA.val> headB.val){
                tmpHead.next=headB;
                headB=headB.next;
            }
            else{
                tmpHead.next=headA;

                headA=headA.next;
            }
            tmpHead=tmpHead.next;

        }
        if(headA!=null){
            tmpHead.next=headA;
        }
        if(headB!=null){
            tmpHead.next=headB;
        }

        return newHead.next;

    }
    public static void main(String[] args) {
        mysinglelist list1=new mysinglelist();
        list1.addEnd(50);
        list1.addEnd(90);
        list1.addEnd(80);
        list1.addEnd(30);
        list1.addEnd(40);

        mysinglelist list2=new mysinglelist();
        list2.addEnd(20);
        list2.addEnd(40);
        list2.addEnd(60);
        list2.addEnd(80);
        list2.addEnd(100);
//        list.creatList();
//        list.show();

//        mysinglelist.ListNode newhead = list.reverseList();
//        list.show(newhead);
//        mysinglelist.ListNode midnode = list.getmidnode();
//        list.show(midnode);
//        System.out.println(midnode.val);
//        System.out.println(list.getrevknode(5).val);
//        list1.show();
//        list2.show();

//        mysinglelist.ListNode merge2 = merge2(list1.head, list2.head);
//        mysinglelist.ListNode merge1 = merge1(list1, list2);

//        list1.show(merge2);
//        list1.show(merge1);
        mysinglelist.ListNode partition = list1.partition(list1.head, 45);
        list1.show(partition);
    }

}
